package com.hillky.emos.wx.controller;

import cn.hutool.json.JSONUtil;
import com.hillky.emos.wx.config.shiro.JwtUtil;
import com.hillky.emos.wx.controller.form.LoginForm;
import com.hillky.emos.wx.controller.form.RegisterForm;
import com.hillky.emos.wx.controller.form.SearchMembersForm;
import com.hillky.emos.wx.controller.form.SearchUserGroupByDeptForm;
import com.hillky.emos.wx.exception.EmosException;
import com.hillky.emos.wx.service.TbUserService;
import com.hillky.emos.wx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api("用户模块Web接口")
public class UserController {

    @Autowired
    private TbUserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${emos.jwt.cache-expire}")
    private int cacheExpire;


    @PostMapping("/login")
    @ApiOperation("登录系统")
    public R login(@RequestBody @Valid LoginForm loginForm){
        Integer userId = userService.login(loginForm.getCode());
        String token = jwtUtil.createToken(userId);
        Set<String> permissions = userService.searchUserPermissions(userId);
        return R.ok("登录成功").put("token",token).put("permission",permissions);
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public R register(@RequestBody @Valid RegisterForm registerForm){
        Integer userId = userService.registerUser(registerForm.getRegisterCode(), registerForm.getCode(), registerForm.getNickname(), registerForm.getPhoto());
        String token = jwtUtil.createToken(userId);
        Set<String> permissions = userService.searchUserPermissions(userId);
        //保存token信息到缓存中
        saveCacheToken(token,userId);
        return R.ok("用户注册成功").put("token",token).put("permission",permissions);
    }

    @GetMapping("/addUser")
    @ApiOperation("添加用户")
    @RequiresPermissions(value = {"ROOT","B"},logical = Logical.OR)
    public R addUser(){
        return R.ok("添加用户成功");
    }

    @GetMapping("/searchUserSummary")
    @ApiOperation("查询用户摘要信息")
    public R searchUserSummary(@RequestHeader("token") String token){
        int userId=jwtUtil.getUserId(token);
        HashMap map=userService.searchUserSummary(userId);
        return R.ok().put("result",map);
    }

    @PostMapping("/searchUserGroupByDept")
    @ApiOperation("查询员工列表，按照部门分组排列")
    @RequiresPermissions(value = {"ROOT","EMPLOYEE:SELECT"},logical = Logical.OR)
    public R searchUserGroupByDept(@Valid @RequestBody SearchUserGroupByDeptForm form){
        ArrayList<HashMap> list=userService.searchUserGroupByDept(form.getKeyword());
        return R.ok().put("result",list);
    }


    @PostMapping("/searchMembers")
    @ApiOperation("查询成员")
    @RequiresPermissions(value = {"ROOT", "MEETING:INSERT", "MEETING:UPDATE"},logical = Logical.OR)
    public R searchMembers(@Valid @RequestBody SearchMembersForm form){
        if(!JSONUtil.isJsonArray(form.getMembers())){
            throw new EmosException("members不是JSON数组");
        }
        List param=JSONUtil.parseArray(form.getMembers()).toList(Integer.class);
        ArrayList list=userService.searchMembers(param);
        return R.ok().put("result",list);
    }


    private void saveCacheToken(String token,Integer userId) {
        redisTemplate.opsForValue().set(token,userId+"",cacheExpire, TimeUnit.DAYS);
    }


}
